<?php
include '../class/class.usulan.php';
include '../class/class.detail.php';
include '../class/class.pegawai.php';
include '../class/class.refcuti.php';
include '../class/class.tujuan.php';
include '../class/class.kanwil.php';
include '../class/class.level.php';
include '../class/class.user.php';

if(isset($_REQUEST['get_all'])){
	session_start();
	$id_kewenangan=$_SESSION['id_kewenangan'];
	$id_user=$_SESSION['id_user'];

	$user=new User();
	$user->id_user=$id_user;

	$userDao=new UserDao();
	$user_nya=$userDao->getUserById($user);

	$usulanDao=new UsulanDao();
	$daftar=array();

	$detailDao=array();
	$pegawaiDao=array();
	$cutiDao=array();
	$levelDao=array();
	$tujuanDao=array();
	$kanwilDao=array();
	$detail=array();
	$msg=true;
	$x=0;
	if($id_kewenangan=='1'){
		$all_usulan=$usulanDao->getAllUsulan();
	}else{
		$all_usulan=$usulanDao->getAll($id_kewenangan,$user_nya['kdkanwil']);
	}
	
	for($i=0;$i<count($all_usulan);$i++){
		$detailDao[$i]=new DetailDao();
		$detail[$i]=$detailDao[$i]->getDetailByIdUsulan($all_usulan[$i]['id_usulan']);
		if($detail[$i]!=false){
			for($j=0;$j<count($detail[$i]);$j++){
				//$kanwilDao[$x]=new KanwilDao();
				$cutiDao[$x]=new RefCutiDao();
				$tujuanDao[$x]=new TujuanDao();
				$levelDao[$x]=new LevelDao();
				$pegawaiDao[$x]=new PegawaiDao();
				$daftar[$x]=array(
					'no_surat' => $all_usulan[$i]['no_surat'],
					//'kanwil' => $kanwilDao[$x]->getNmKanwil($all_usulan[$i]['kdkanwil']),
					'nip' => $detail[$i][$j]['nip'],
					'nama' => $pegawaiDao[$x]->getNmPegawai($detail[$i][$j]['nip']),
					'id_cuti' => $cutiDao[$x]->getCuti($detail[$i][$j]['id_cuti']),
					'id_tujuan' => $tujuanDao[$x]->getTujuan($detail[$i][$j]['id_tujuan']),
					'tgl_mulai' => $detail[$i][$j]['tgl_mulai'],
					'tgl_selesai' => $detail[$i][$j]['tgl_selesai'],
					'level' => $levelDao[$x]->getLevel($detail[$i][$j]['id_level']),
					'id_det_usulan' => $detail[$i][$j]['id_det_usulan'],
					);
				$x++;
			}
		}else{
			$kanwilDao[$x]=new KanwilDao();
			$daftar[$x]=array(
				'no_surat' => $all_usulan[$i]['no_surat'],
				//'kanwil' => $kanwilDao[$x]->getNmKanwil($all_usulan[$i]['kdkanwil']),
				'nip' => 'Kosong',
				'nama' => 'Kosong',
				'id_cuti' =>'Kosong',
				'id_tujuan' => 'Kosong',
				'tgl_mulai' => 'Kosong',
				'tgl_selesai' => 'Kosong',
				'level' => 'Belum Entri Pegawai',
				);
			$x++;
		}
	}

	$data=array(
		'msg' => $msg,
		'daftar' => $daftar,
		);

	echo json_encode($data);
}

if(isset($_REQUEST['aksi_notifikasi'])){
	session_start();

	$id_kewenangan=$_SESSION['id_kewenangan'];

	$id_user=$_SESSION['id_user'];
	$msg=false;
	$user=new User();
	$user->id_user=$id_user;
	$usulan=array();
	$userDao=new UserDao();
	$pesan=' Usulan Baru';
	$user_nya=$userDao->getUserById($user);

	$usulanDao= new UsulanDao();
	if($id_kewenangan=='1'){
		$usulan=$usulanDao->getAllNotifikasi();
		if(count($usulan)>0){
			$msg=true;
		}
	}else{
		$usulan=$usulanDao->getNotifikasi($user_nya['kdkanwil']);

		if(count($usulan)>0){
			$msg=true;
			$detaildao=array();
			$detaildao2=array();
			$detaildao3=array();
			$detaildao4=array();
			$detTolak=array();
			$detkonfirmasi=array();
			$detSetujui=array();
			$detTerima=array();
			$t=0;
			$k=0;
			$s=0;
			$y=0;
			$pesan=' Usulan Diterima Kepegawaian';
			for($i=0;$i<count($usulan);$i++){
				$detaildao[$i]=new DetailDao();
				$detaildao2[$i]=new DetailDao();
				$detaildao3[$i]=new DetailDao();
				$detaildao4[$i]=new DetailDao();
				$detTolak[$i]=$detaildao[$i]->cekDetail($usulan[$i]['id_usulan'],'2');
				
				if($detTolak[$i]!=false){
					$t+=count($detTolak[$i]);
				}
				$detkonfirmasi[$i]=$detaildao2[$i]->cekDetail($usulan[$i]['id_usulan'],'3');
				
				if($detkonfirmasi[$i]!=false){
					$k+=count($detkonfirmasi[$i]);
				}
				$detSetujui[$i]=$detaildao3[$i]->cekDetail($usulan[$i]['id_usulan'],'6');
				
				if($detSetujui[$i]!=false){
					$s+=count($detSetujui[$i]);
				}
				$detTerima[$i]=$detaildao4[$i]->cekDetail($usulan[$i]['id_usulan'],'4');
				if($detTerima[$i]!=false){
					$y+=count($detTerima[$i]);
				}

			}
		}
		$pesan.='<br /> '.$t.' Pegawai Ditolak';
		$pesan.='<br /> '.$k.' Pegawai Perlu Konfirmasi';
		$pesan.='<br /> '.$y.' Pegawai Diterima Kepegawaian';
		$pesan.='<br /> '.$s.' Pegawai Diterima Setjen';
		
	}


	

	$data=array(
		'msg' => $msg,
		'pesan' => count($usulan).''.$pesan,
		);

	echo json_encode($data);
}
?>